<template>
  <div class="xtx-goods-page" v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">{{ goods.categories[1].name }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">{{ goods.categories[0].name }}</XtxBreadItem>
        <XtxBreadItem>{{ goods.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 商品信息左边 -->
        <div class="goods-info-left">
          <GoodsImage :images="goods.mainPictures" />
          <GoodsSales />
        </div>
        <!-- 商品信息右边 -->
        <div class="goods-info-right">
          <GoodsName :goods="goods" />
          <!-- 如果 :key 属性不同，组件就会被重新创建 -->
          <GoodsSku skuId="1369155864430120962" @change="getSkuChange" :key="goods.id" :goods="goods" />
          <XtxNumbox v-model="count" :max="goods.inventory" label="数量" />
          <XtxButton @click="addToCartBtn" type="primary" style="margin-top:20px">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="goods.id" />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTabs />
          <!-- 注意事项 -->
          <GoodsWarn />
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :goodsId="goods.id" :type="1" />
          <GoodsHot :goodsId="goods.id" :type="2" style="margin-top:20px" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodsRelevant from './components/goods-relevant.vue'
import { findGoods } from '@/api/goods'
import { useRoute } from 'vue-router'
import { provide, ref, watch } from 'vue'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsHot from './components/goods-hot.vue'
import GoodsWarn from './components/goods-warn.vue'
import { Message } from '@/components/library/Message'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: { GoodsRelevant, GoodsImage, GoodsSales, GoodsName, GoodsSku, GoodsTabs, GoodsHot, GoodsWarn },
  setup () {
    const route = useRoute()
    const goods = ref(null)
    // 注意单页面应用路由id变化，需要重新获取商品详情
    watch(() => route.params.id, async (id) => {
      // 如果不是商品详情页不发送请求
      if (route.path !== `/product/${id}`) return
      // 根据商品 id 获取商品数据
      const { result } = await findGoods(id)
      goods.value = result
      // 打印测试用的
      // console.log(result)
    }, { immediate: true })

    // 商品数量
    const count = ref(1)

    // sku改变时候触发
    const getSkuChange = (val) => {
      // console.log(val)
      // 如果子组件回传 skuId，更新商品库存和价格
      if (val.skuId) {
        goods.value.skuId = val.skuId
        goods.value.attrsText = val.attrsText
        goods.value.inventory = val.inventory
        goods.value.price = val.price
        goods.value.oldPrice = val.oldPrice
        // console.log(val.attrsText)
      }
    }

    // 页面组件提供数据，所有后代组件都能访问
    provide('goods', goods)

    const store = useStore()
    // 加入购物车按钮
    const addToCartBtn = () => {
      // console.log('加入购物车业务开始', goods.value)
      const { id, name, mainPictures, price, inventory, skuId, attrsText } = goods.value
      // 判断是否选择了完整规格，没有 skuId 就提示用户
      if (!skuId) return Message({ type: 'warn', message: '请选择商品规格' })
      // 整个价格购物车商品的数据结构要参考 - 购物车列表的字段名
      const goodsItem = {
        id: id, // SPUID
        name: name, // 商品名称
        picture: mainPictures[0], // 图片
        price: price, // 加入时价格
        nowPrice: price, // 当前的价格
        stock: inventory, // 库存
        count: count.value, // 数量
        skuId: skuId, // SKUID
        attrsText: attrsText, // 规格文本
        selected: true, // 是否选中
        isEffective: true // 是否为有效商品
      }
      console.log(goodsItem)
      // 💥 因为购物车会分为本地版和服务器版，涉及到异步请求，所以要升级成 dispatch 调用 actions
      store.dispatch('cart/insertCart', goodsItem).then(res => {
        Message({ type: 'success', message: res })
      })
    }

    // 返回给模板使用
    return { goods, count, getSkuChange, addToCartBtn }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .goods-info-left {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .goods-info-right {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
